package com.cfweb.controller.app;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.cfweb.api.ServiceApi;
import com.cfweb.dao.CfNullServiceInstanceMapper;
import com.cfweb.dao.CfServiceInstanceMapper;
import com.cfweb.domain.CfServiceInstance;
import com.cfweb.domain.CfServiceInstanceExample;
import com.cfweb.util.DbUtil;
import com.cfweb.util.ExtJSResponse;

@Service
@Path("/serviceaudit")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ServiceAuditManagement {
	private String null_plan;
	private String null_planid;

	@Value("${null_plan}")
	public void setNull_plan(String null_plan) {
		this.null_plan = null_plan;
	}

	@Value("${null_planid}")
	public void setNull_planid(String null_planid) {
		this.null_planid = null_planid;
	}
	/**
	 * session
	 */
	@Autowired
	private HttpSession session;
	/**
	 * 服务实例
	 */
	@Autowired
	private CfServiceInstanceMapper cfServiceInstanceMapper;
	@Autowired
	private CfNullServiceInstanceMapper cfNullServiceInstanceMapper;

	/**
	 * 列出所有待审核的服务
	 * 
	 * @return
	 * @throws Exception
	 */
	@Path("/listServiceOfAudit")
	@GET
	public ExtJSResponse listServiceOfAudit() throws Exception {
		CfServiceInstanceExample cfServiceInstanceExample = new CfServiceInstanceExample();
		Integer[] states = {0,1};
		List<Integer> values = Arrays.asList(states);
		cfServiceInstanceExample.createCriteria().andStateIn(values);
		List<CfServiceInstance> result = cfServiceInstanceMapper.selectByExample(cfServiceInstanceExample);
		return ExtJSResponse.successResWithData(result);
	}


	@Path("/disagree")
	@GET
	public ExtJSResponse disagree(@QueryParam("id") Integer id) throws Exception {
		CfServiceInstance cfServiceInstance = cfServiceInstanceMapper.selectByPrimaryKey(id);
		if (cfServiceInstance == null) {
			return ExtJSResponse.errorResWithMsg("数据不存在");
		}
		cfServiceInstance.setState(1);
		int result = cfServiceInstanceMapper.updateByPrimaryKey(cfServiceInstance);
		if (result != 1) {
			return ExtJSResponse.errorResWithMsg("更新失败");
		}
		return ExtJSResponse.successResWithData("更新成功");
	}

	@Path("/agree")
	@GET
	public ExtJSResponse agree(@QueryParam("id") Integer id) throws Exception {
		CfServiceInstance cfServiceInstance = cfServiceInstanceMapper.selectByPrimaryKey(id);
		if (cfServiceInstance == null) {
			return ExtJSResponse.errorResWithMsg("数据不存在");
		}
		// 创建 服务实例
		boolean flag = ServiceApi.createServiceInstanceByOrgAndSpace(session, cfServiceInstance.getOrg(),
				cfServiceInstance.getSpace(), cfServiceInstance.getService(), cfServiceInstance.getPlanId(),
				cfServiceInstance.getServiceInstanceName(), true);
		System.out.println(flag);
		if (cfServiceInstance.getState() != 2 && flag) {
			if (cfServiceInstance.getPlan().contains("medium")) {
				// 创建一个空服务
				ServiceApi.createNullService(cfNullServiceInstanceMapper, cfServiceInstance.getServiceInstanceName(),
						session, cfServiceInstance.getOrg(), cfServiceInstance.getSpace(), 1,null_plan, null_planid);
			} else if (cfServiceInstance.getPlan().contains("large")) {
				// 创建两个空服务
				ServiceApi.createNullService(cfNullServiceInstanceMapper, cfServiceInstance.getServiceInstanceName(),
						session, cfServiceInstance.getOrg(), cfServiceInstance.getSpace(), 2,null_plan, null_planid);
			}

			cfServiceInstance.setState(2);
			// 通过jdbc链接，获取host port user
			Thread.sleep(1000);
			ServiceApi.getHostPortUser(cfServiceInstance);
			cfServiceInstanceMapper.updateByPrimaryKey(cfServiceInstance);
			return ExtJSResponse.successResWithData("创建成功");
		}
		return ExtJSResponse.successResWithData("创建失败");
	}

	/**
	 * 删除待审核的服务
	 * 
	 * @param id
	 * @return
	 */
	@Path("/delete")
	@GET
	public ExtJSResponse delete(@QueryParam("id") int id) {
		cfServiceInstanceMapper.deleteByPrimaryKey(id);
		return ExtJSResponse.successResWithData("删除成功");
	}
}
